<html>
<head>
	<title id="title">Joel</title>
	
	<script type="text/javascript" src="mootools.js"></script>
	<script type="text/javascript" src="request.js"></script>
	<script type="text/javascript" src="class.loader.js"></script>
	<script type="text/javascript" src="class.broadcaster.js"></script>
	<script type="text/javascript" src="app.js"></script>
	
	
	<link href="index.css" rel="stylesheet" type="text/css">
	<script>


	// initialise database
	var myDB = Titanium.Database.open('Joel');
	myDB.execute('CREATE  TABLE  IF NOT EXISTS `user` ("user_id" INTEGER PRIMARY KEY  NOT NULL , "user_name" VARCHAR, "user_email" VARCHAR, "user_password" VARCHAR, "user_autologin" INTEGER)');
	myDB.execute('CREATE  TABLE  IF NOT EXISTS `tasks` ("task_id" INTEGER PRIMARY KEY  NOT NULL , "task_description" VARCHAR, "task_name" VARCHAR, "task_orgiest" INTEGER, "task_currest" INTEGER, "task_elapsed" INTEGER, "task_prio" INTEGER)');
	myDB.execute('CREATE  TABLE  IF NOT EXISTS `session` ("user_id" INTEGER PRIMARY KEY  NOT NULL , "login_time" INTEGER, "session_id" VARCHAR, "reconnect" INTEGER)');
	
	var status = false;
	var viewCollections = Titanium.UI.createWebView({url:'collections.html', name:'viewCollections'});
	var viewLogin = Titanium.UI.createWebView({url:'login.html', name:'viewLogin'});
	
	Titanium.UI.currentWindow.addView(viewCollections);
	Titanium.UI.currentWindow.addView(viewLogin);
	
	
	window.onload = function () {
		$('status').innerHTML = 'Connecting Service';
		
		request('init', '', function(res) {
			if(res.loggedin) {
				Titanium.UI.currentWindow.showView(viewCollections,{
					animated:true,
					animationStyle:Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT,
					animationDuration:500
				});
				
			} else {
				Titanium.App.Properties.setString('sessionid', res.sessid);
				Titanium.UI.currentWindow.showView(viewLogin,{
					animated:true,
					animationStyle:Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT,
					animationDuration:500
				});
			}
		});
	};
	
	</script>
</head>
<body>
	
	<div id="status"></div>
	
</body>
</html>